import React, { Component } from 'react';
import '../css/login.css';
import $ from 'jquery';
function getCookie(name){
	var str = document.cookie;
	var arr = str.split("; ");
	for(var i = 0; i < arr.length; i++){
		var arr1 = arr[i].split("=");
		if(arr1[0]==name){
			return arr1[1];
		}
	}
}
function setCookie(name,val,n){
	var oDate = new Date();
	oDate.setDate(oDate.getDate()+n);
	document.cookie = name + "=" + val + ";expires="+ oDate ;
}
function removeCookie(name){
	setCookie(name,1,-1);
}


class Login extends React.Component{
	constructor(props){
		super(props)
	
	}
	login(){
		var value=$('.username').val();
				if(value==""){
					alert("请输入用户名")
					return
			
				}
				var _this=this;
				$.ajax({
					type:'get',
					url:'http://jx.xuzhixiang.top/ap/api/login.php',
					data:{username:$('.username').val(),password:$('.password').val()},
					async:false,
					dataType:'json',
					success:function(data){
						console.log(data)
						if(data.code==1){
							setCookie("token",data.data.id,{expires:30});
							setCookie("mingzi",data.data.username,{expires:30});
							alert("登录成功，跳转主页");
							window.location.reload()

							window.location.href="#/home"
							
	
						}else{
							alert("登录失败，请重新登录");
							window.location.href="#/login"
						
	
						 }
						
					}
					
				})
	}
	gozhuce(){
		this.props.history.push('/register')
	}
	render(){
		var _this=this;
		return(
			<div className='login'>
				<header className='login-header'>
					<span></span>
					<div>
						<h1>药素网</h1>
						<span>药.好.全 | 全.好.药</span>
					</div>
				</header>
				<div className='login-section'>
					<input type='text' className="username" placeholder='请输入手机号码'/>
					<input type='text' className="password" placeholder='请输入密码'/>
					<h4>忘记密码？</h4>
					<input type='button' className="login-l" value='登录' onClick={this.login.bind(this)}/>
					<input type='button' value='立即注册' onClick={this.gozhuce.bind(this)}/>
					<h3>《隐私政策》</h3>
				</div>
			</div>
		)
	}
}

export default Login;